package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Examine;
import com.ruoyi.system.domain.InboundTouristTotalStatistics;
import com.ruoyi.system.domain.vo.FileUploadVO;
import com.ruoyi.system.mapper.ExamineMapper;
import com.ruoyi.system.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StatisticsMapper;
import com.ruoyi.system.domain.Statistics;
import com.ruoyi.system.service.IStatisticsService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 入境旅游包机奖励统计Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, Statistics> implements IStatisticsService {
    @Autowired
    private StatisticsMapper statisticsMapper;

    @Resource
    private FileUploadService fileUploadService;

    @Resource
    private ExamineMapper examineMapper;

    /**
     * 查询入境旅游包机奖励统计
     *
     * @param id 入境旅游包机奖励统计主键
     * @return 入境旅游包机奖励统计
     */
    @Override
    public Statistics selectStatisticsById(Long id) {

        Statistics statistics = statisticsMapper.selectStatisticsById(id);
        List<FileUploadVO> list = fileUploadService.listFileUploadsByBusinessId(statistics.getBusinessId());
        statistics.setFiles(list);
        List<Examine> examines = statistics.getExamines();
        examines.sort((e1, e2) -> e2.getCreateTime().compareTo(e1.getCreateTime()));
        return statistics;
    }

    /**
     * 查询入境旅游包机奖励统计列表
     *
     * @param statistics 入境旅游包机奖励统计
     * @return 入境旅游包机奖励统计
     */
    @Override
    public List<Statistics> selectStatisticsList(Statistics statistics) {
        startPage();
        QueryWrapper<Statistics> statisticsQueryWrapper = new QueryWrapper<>(statistics);
        List<Statistics> statisticsList  = new ArrayList<>();
        statisticsQueryWrapper.orderByDesc("created_at");
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            statisticsList = statisticsMapper.selectList(statisticsQueryWrapper);
        } else {
            statisticsQueryWrapper.eq("create_by", SecurityUtils.getUserId().toString());
            statisticsList = statisticsMapper.selectList(statisticsQueryWrapper);
        }
//        if(statisticsList.size()>0){
//            statisticsList.forEach(row -> {
//                List<Examine> examines = row.getId()  == null ? new ArrayList<>() : examineMapper.selectList(new QueryWrapper<Examine>().eq("business_id", row.getId()).eq(StringUtils.isNotEmpty(row.getStatus()), "status", row.getStatus()).orderByDesc("create_time"));
//                row.setExamines(examines);
//            });
//        }
        return statisticsList;
    }

    /**
     * 新增入境旅游包机奖励统计
     *
     * @param statistics 入境旅游包机奖励统计
     * @return 结果
     */
    @Override
    public int insertStatistics(Statistics statistics) {
        statistics.setCreateBy(SecurityUtils.getUserId().toString());
        statistics.setCreatedAt(new Date());
        statistics.setStatus(0);
        int insert = statisticsMapper.insert(statistics);
        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(statistics.getId())).setStatus(0).setType(statistics.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(statistics.getRemark());
        examineMapper.insert(examine);

        return insert;
    }

    /**
     * 修改入境旅游包机奖励统计
     *
     * @param statistics 入境旅游包机奖励统计
     * @return 结果
     */
    @Override
    public int updateStatistics(Statistics statistics) {

        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(statistics.getId())).setStatus(statistics.getStatus()).setType(statistics.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(statistics.getRemark());
        examineMapper.insert(examine);
        return statisticsMapper.updateById(statistics);
    }

    /**
     * 批量删除入境旅游包机奖励统计
     *
     * @param ids 需要删除的入境旅游包机奖励统计主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsByIds(Long[] ids) {
        return statisticsMapper.deleteStatisticsByIds(ids);
    }

    /**
     * 删除入境旅游包机奖励统计信息
     *
     * @param id 入境旅游包机奖励统计主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsById(Long id) {
        return statisticsMapper.deleteStatisticsById(id);
    }
}
